<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

// 表单数据
const username = ref('')
const password = ref('')
const rememberMe = ref(false)

// 错误信息状态
const errors = reactive({
  username: '',
  password: '',
  general: ''
})

// 加载状态
const isLoading = ref(false)

// 路由实例
const router = useRouter()

// 处理输入变化，清除对应字段的错误
const handleInput = (field) => {
  errors[field] = ''
  errors.general = ''
}

// 验证表单
const validateForm = () => {
  let isValid = true
  
  // 清除之前的错误
  errors.username = ''
  errors.password = ''
  errors.general = ''
  
  // 验证用户名
  if (!username.value.trim()) {
    errors.username = '用户名不能为空'
    isValid = false
  }
  
  // 验证密码
  if (!password.value) {
    errors.password = '密码不能为空'
    isValid = false
  } else if (password.value.length < 6) {
    errors.password = '密码长度不能少于6位'
    isValid = false
  }
  
  return isValid
}

// 处理登录表单提交
const handleLogin = async (event) => {
  event.preventDefault()
  
  // 验证表单
  if (!validateForm()) {
    return
  }
  
  // 设置加载状态
  isLoading.value = true
  
  try {
    // 模拟API请求延迟
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 模拟登录逻辑
    if (username.value === 'admin' && password.value === 'password') {
      // 存储登录状态
      if (rememberMe.value) {
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('username', username.value)
      } else {
        sessionStorage.setItem('isLoggedIn', 'true')
        sessionStorage.setItem('username', username.value)
      }
      
      // 登录成功后跳转到商城页面
      router.push('/mall')
    } else {
      errors.general = '用户名或密码错误，请重试'
      // 清空密码输入框
      password.value = ''
    }
  } catch (error) {
    errors.general = '登录失败，请稍后重试'
  } finally {
    // 重置加载状态
    isLoading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-form">
      <h2>用户登录</h2>
      
      <form @submit="handleLogin">
        <!-- 用户名输入 -->
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            @input="handleInput('username')"
            placeholder="请输入用户名"
            :class="['form-input', { 'input-error': errors.username }]"
            :disabled="isLoading"
          />
          <div v-if="errors.username" class="field-error">
            {{ errors.username }}
          </div>
        </div>
        
        <!-- 密码输入 -->
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            @input="handleInput('password')"
            placeholder="请输入密码"
            :class="['form-input', { 'input-error': errors.password }]"
            :disabled="isLoading"
          />
          <div v-if="errors.password" class="field-error">
            {{ errors.password }}
          </div>
        </div>
        
        <!-- 记住我选项 -->
        <div class="form-group remember-me">
          <input
            type="checkbox"
            id="remember-me"
            v-model="rememberMe"
            :disabled="isLoading"
          />
          <label for="remember-me">记住我</label>
        </div>
        
        <!-- 通用错误提示 -->
        <div v-if="errors.general" class="error-message">
          {{ errors.general }}
        </div>
        
        <!-- 登录按钮 -->
        <button 
          type="submit" 
          class="login-button"
          :disabled="isLoading"
        >
          <span v-if="isLoading" class="loading-spinner"></span>
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.login-form {
  background: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #555;
}

.form-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.1);
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.remember-me input {
  margin-right: 8px;
}

.error-message {
  color: #f44336;
  text-align: center;
  margin-bottom: 20px;
  padding: 10px;
  background-color: #ffebee;
  border-radius: 4px;
  animation: shake 0.3s ease-in-out;
}

.field-error {
  color: #f44336;
  font-size: 12px;
  margin-top: 5px;
  display: block;
}

.input-error {
  border-color: #f44336 !important;
  box-shadow: 0 0 0 2px rgba(244, 67, 54, 0.1) !important;
}

.loading-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #45a049;
}

.login-button:active {
  background-color: #3d8b40;
}
</style>